<?php
class Charting extends Model{
    function chart_deal($interval,$broad,$rev){
        $data  =  "http://chart.apis.google.com/chart";
        $data .=  "?chxl=1:";
        $deal = $this->reports->getDeal($this->input->post('month'),$this->input->post('year'));
        foreach($deal as $row):
            $data .='|'.$row['username'];
        endforeach;
        
        $data .=  "&chxr=0,0,".$interval;
        $data .=  "&chxs=0,676767,11.5,0,lt,676767";
        $data .=  "&chxt=y,x";
        $data .=  "&chbh=a,0,0";
        $data .=  "&chs=".$broad."";
        $data .=  "&cht=bvg";
        $data .=  "&chco=3072F3,FF0000";
        $data .=  "&chds=0,1000000000,0,1000000000";
        $data .=  "&chd=t:";
        //&chd=t:10,50|20,54
        $count=COUNT($deal);
        
        $i=1;
        foreach($deal as $row):
        if($row['total']<>""):
        $data .=  $row['total'];
        else:
        $data .= 0;
        endif;
        
        if($i<$count){
        $data .=  ",";
        }
        $i++;
        endforeach;
        
        $data .=  "|";
        
        $i=1;
        foreach($deal as $row):
        if($row['target']<>""):
        $data .=  $row['target'];
        else:
        $data .= 0;
        endif;
        
        if($i<$count){
        $data .=  ",";
        }
        $i++;
        endforeach;
        
        $data .=  "&chdl=".$rev;
        $data .=  "&chdlp=t";
        $data .=  "&chg=-1,10";
        $data .=  "&chma=|15";
        $data .=  "&chtt=Deal+Project";
        $data .=  "&chts=676767,10";
        return $data;
    }
    
    function chartdeal($title,$name){
        
        //$url = file_get_contents($this->chart_deal('|MKI|PRO',200,'600x320','10,50','20,54','Rev|Target'));
        $url=$this->chart_deal(1000000000,'600x420','Rev|Target');
        $url = file_get_contents($url);  
        $img = fopen($name.".png", 'w');
        fwrite($img, $url);
        fclose($img);
    }
    
    function chart3_deal($interval,$broad,$rev){
        if(($this->input->post('month')=='01') || ($this->input->post('month')=='02') || ($this->input->post('month')=='03') ):
            $month1='01';
            $month2='02';
            $month3='03';
        elseif(($this->input->post('month')=='04') || ($this->input->post('month')=='05') || ($this->input->post('month')=='06')):    
            $month1='04';
            $month2='05';
            $month3='06';
        elseif(($this->input->post('month')=='07') || ($this->input->post('month')=='08') || ($this->input->post('month')=='09')):    
            $month1='07';
            $month2='08';
            $month3='09';
        elseif(($this->input->post('month')=='10') || ($this->input->post('month')=='11') || ($this->input->post('month')=='12')):    
            $month1='10';
            $month2='11';
            $month3='12';         
        endif;
            
        $data  =  "http://chart.apis.google.com/chart";
        $data .=  "?chxl=1:";
        $month = $this->reports->getDealMonthly($month1,$month2,$month3,$this->input->post('year'));
        foreach($month as $row):
            $data .='|'.$row['username'];
        endforeach;
        
        //$data .=  "&chxr=0,0,".$interval;
        $data .=  "&chxr=0,0,".$interval."|1,0,0";
        //$data .=  "&chxs=0,676767,11.5,0,lt,676767";
        $data .= "&chxs=0,676767,11.5,1,l,676767|1,676767,15,1,lt,676767";
        $data .= "&chxtc=1,0";
        $data .= "&chxt=y,x";
        //$data .= "&chbh=a,0,0";
        $data .= "&chbh=a";
        //$data .=  "&chbh=a,0,0,0";
        $data .=  "&chs=".$broad."";
        $data .=  "&cht=bvg";
        $data .=  "&chco=3072F3,FF0000,3D7930";
        $data .=  "&chds=0,1000000000,0,1000000000,0,1000000000";
        $data .=  "&chd=t:";
        //&chd=t:10,50|20,54
        $count=COUNT($month);
        
        $i=1;
        foreach($month as $row):
        if($row['month_1']<>""):
        $data .=  $row['month_1'];
        else:
        $data .= 0;
        endif;
        
        if($i<$count){
        $data .=  ",";
        }
        $i++;
        endforeach;
        
        $data .=  "|";
        
        $i=1;
        foreach($month as $row):
        if($row['month_2']<>""):
        $data .=  $row['month_2'];
        else:
        $data .= 0;
        endif;
        
        if($i<$count){
        $data .=  ",";
        }
        $i++;
        endforeach;
        
        $i=1;
        foreach($month as $row):
        if($row['month_3']<>""):
        $data .=  $row['month_3'];
        else:
        $data .= 0;
        endif;
        
        if($i<$count){
        $data .=  ",";
        }
        $i++;
        endforeach;
        
        $data .=  "&chdl=".$rev;
        $data .=  "&chdlp=r";
        $data .=  "&chg=12,0,0,0";
        $data .=  "&chma=0,0,5|0,5";
        $data .=  "&chtt=Deal+Project+3+Bulan";
        //$data .=  "&chts=676767,10";
        return $data;
    }
    
    function chart3deal($title,$name,$month1,$month2,$month3){
        
        //$url = file_get_contents($this->chart_deal('|MKI|PRO',200,'600x320','10,50','20,54','Rev|Target'));
        $url=$this->chart3_deal(1000000000,'800x325',$month1.'.'.month_name($month1).'|'.$month2.'.'.month_name($month2).'|'.$month3.'.'.month_name($month3));
        $url = file_get_contents($url);  
        $img = fopen($name.".png", 'w');
        fwrite($img, $url);
        fclose($img);
    } 
    
    function chart12_deal($interval,$broad,$rev){    
        $data  =  "http://chart.apis.google.com/chart";
        $data .=  "?chxl=1:";
        $year = $this->reports->getDealYearly($this->input->post('year'));
        foreach($year as $row):
            $data .='|'.$row['username'];
        endforeach;
        
        $data .=  "&chxr=0,0,".$interval;
        //$data .=  "&chxr=0,0,0,".$interval;
        $data .=  "&chxs=0,676767,11.5,0,lt,676767";
        $data .=  "&chxt=y,x";
        $data .=  "&chbh=a,0,0";
        //$data .=  "&chbh=a,0,0,0";
        $data .=  "&chs=".$broad."";
        $data .=  "&cht=bvg";
        $data .=  "&chco=3072F3,FF0000,3D7930,FFCC33,00FF00,990066,BBCCED,8F6189,3399CC,5800AA,DDF8CC,000000";
        $data .=  "&chds=0,1000000000";
        $data .=  "&chd=t:";
        //&chd=t:10,50|20,54
        $count=COUNT($year);
        
        $i=1;
        foreach($year as $row):
        if($row['month_1']<>""):
        $data .=  $row['month_1'];
        else:
        $data .= 0;
        endif;
        
        if($i<$count){
        $data .=  ",";
        }
        $i++;
        endforeach;
        
        $data .=  "|";
        
        $i=1;
        foreach($year as $row):
        if($row['month_2']<>""):
        $data .=  $row['month_2'];
        else:
        $data .= 0;
        endif;
        
        if($i<$count){
        $data .=  ",";
        }
        $i++;
        endforeach;
        
        $i=1;
        foreach($year as $row):
        if($row['month_3']<>""):
        $data .=  $row['month_3'];
        else:
        $data .= 0;
        endif;
        
        if($i<$count){
        $data .=  ",";
        }
        $i++;
        endforeach;
        
        $i=1;
        foreach($year as $row):
        if($row['month_4']<>""):
        $data .=  $row['month_4'];
        else:
        $data .= 0;
        endif;
        
        if($i<$count){
        $data .=  ",";
        }
        $i++;
        endforeach;
        
        $i=1;
        foreach($year as $row):
        if($row['month_5']<>""):
        $data .=  $row['month_5'];
        else:
        $data .= 0;
        endif;
        
        if($i<$count){
        $data .=  ",";
        }
        $i++;
        endforeach;
        
        $i=1;
        foreach($year as $row):
        if($row['month_6']<>""):
        $data .=  $row['month_6'];
        else:
        $data .= 0;
        endif;
        
        if($i<$count){
        $data .=  ",";
        }
        $i++;
        endforeach;
        
        $i=1;
        foreach($year as $row):
        if($row['month_7']<>""):
        $data .=  $row['month_7'];
        else:
        $data .= 0;
        endif;
        
        if($i<$count){
        $data .=  ",";
        }
        $i++;
        endforeach;
        
        $i=1;
        foreach($year as $row):
        if($row['month_8']<>""):
        $data .=  $row['month_8'];
        else:
        $data .= 0;
        endif;
        
        if($i<$count){
        $data .=  ",";
        }
        $i++;
        endforeach;
        
        $i=1;
        foreach($year as $row):
        if($row['month_9']<>""):
        $data .=  $row['month_9'];
        else:
        $data .= 0;
        endif;
        
        if($i<$count){
        $data .=  ",";
        }
        $i++;
        endforeach;
        
        $i=1;
        foreach($year as $row):
        if($row['month_10']<>""):
        $data .=  $row['month_10'];
        else:
        $data .= 0;
        endif;
        
        if($i<$count){
        $data .=  ",";
        }
        $i++;
        endforeach;
        
        $i=1;
        foreach($year as $row):
        if($row['month_11']<>""):
        $data .=  $row['month_11'];
        else:
        $data .= 0;
        endif;
        
        if($i<$count){
        $data .=  ",";
        }
        $i++;
        endforeach;
        
        $i=1;
        foreach($year as $row):
        if($row['month_12']<>""):
        $data .=  $row['month_12'];
        else:
        $data .= 0;
        endif;
        
        if($i<$count){
        $data .=  ",";
        }
        $i++;
        endforeach;
        
        $data .=  "&chdl=".$rev;
        $data .=  "&chdlp=r";
        //$data .=  "&chg=12,0,0,0";
        //$data .=  "&chma=|15";
        //$data .=  "&chma=|0,5";
        $data .=  "&chtt=Deal+Project";
        $data .=  "&chts=676767,10";
        return $data;
    }
    
    function chart12deal($title,$name){
        
        //$url = file_get_contents($this->chart_deal('|MKI|PRO',200,'600x320','10,50','20,54','Rev|Target'));
        $url=$this->chart12_deal(1000000000,'800x250','01'.'.'.month_name('01').'|'.'02'.'.'.month_name('02').'|'.'03'.'.'.month_name('03').'|'.'04'.'.'.month_name('04').'|'.'05'.'.'.month_name('05').'|'.'06'.'.'.month_name('06').'|'.'07'.'.'.month_name('07').'|'.'08'.'.'.month_name('08').'|'.'09'.'.'.month_name('09').'|'.'10'.'.'.month_name('10').'|'.'11'.'.'.month_name('11').'|'.'12'.'.'.month_name('12'));
        $url = file_get_contents($url);  
        $img = fopen($name.".png", 'w');
        fwrite($img, $url);
        fclose($img);
    }  
}